from django.contrib.auth.models import Permission, Group
from apps.users.models import User
from django.contrib.contenttypes.models import ContentType
from rest_framework import serializers

from apps.goods.models import SPU, SPUSpecification, SpecificationOption, SKUImage, SKU, Brand, GoodsCategory, \
    GoodsChannel, GoodsChannelGroup

#SPU
from apps.orders.models import OrderInfo, OrderGoods


class My_all_spuSerializer(serializers.ModelSerializer):
    brand=serializers.StringRelatedField(read_only=True)
    brand_id=serializers.IntegerField()
    category1_id=serializers.IntegerField()
    category2_id=serializers.IntegerField()
    category3_id=serializers.IntegerField()
    category1 = serializers.StringRelatedField()
    category2 = serializers.StringRelatedField()
    category3 = serializers.StringRelatedField()
    class Meta:
        model = SPU
        fields = '__all__'

# 新增商品规格选项
class ADD_SpecificationOption_serializer(serializers.ModelSerializer):
    class Meta:
        model = SpecificationOption
        fields = ['id', 'value']
'''通过嵌套序列化器,得到相应对象规格的所有规格选项'''
#商品规格
class SPU_specification_serializer(serializers.ModelSerializer):
    spu = serializers.StringRelatedField(read_only=True)
    spu_id = serializers.IntegerField()
    options = ADD_SpecificationOption_serializer(read_only=True, many=True)
    class Meta:
        model = SPUSpecification
        fields = '__all__'


#商品规格选项管理
class SpecificationOption_serializer(serializers.ModelSerializer):
    spec = serializers.StringRelatedField(read_only=True)
    spec_id = serializers.IntegerField()

    class Meta:

        model=SpecificationOption
        fields='__all__'

#新增SPU表中的获取品牌信息
class ADD_SPU_brand_Serializer(serializers.ModelSerializer):
    class Meta:
        model = Brand
        fields = ['id','name']


#新增SPU表中的获取分类信息
class SPU_category1_serialize(serializers.ModelSerializer):
    # id = serializers.IntegerField(write_only=True)
    class Meta:
        model = GoodsCategory
        fields = '__all__'



#图片是商品表中的外键
class order_goods_picture_serializer(serializers.ModelSerializer):
    class Meta:
        model = SKU
        fields = ['name','default_image']
#订单视图商品表嵌套序列化器
class order_goods_serializer(serializers.ModelSerializer):
    sku = order_goods_picture_serializer(read_only=True)
    class Meta:
        model = OrderGoods
        fields = ['count','price','sku']
#订单视图集序列化器
class Order_serializer(serializers.ModelSerializer):
    #一个订单有多个商品信息
    skus = order_goods_serializer(many=True,read_only=True)
    class Meta:
        model = OrderInfo
        fields = '__all__'





#权限管理
class PermissionSerializer(serializers.ModelSerializer):
    class Meta:
        model = Permission
        fields = '__all__'
#获取权限类型
class ContentType_Serializer(serializers.ModelSerializer):
    class Meta:
        model = ContentType
        fields = ['id','name']
#用户组管理
class GroupSerializer(serializers.ModelSerializer):
    class Meta:
        model = Group
        fields = '__all__'


#管理员管理
class AdminSerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'
        extra_kwargs={
            'password':{
                'write_only':True
            }
        }
    #需要重写create方法
    def create(self, validated_data):
        #继承原来的方法创建完对象之后
        admin = super().create(validated_data)
        #这里新增添加密码加密
        password =validated_data['password']
        admin.set_password(password)
        admin.is_staff = True
        admin.save()
        #因为是创建管理员所以还需要让is_staff = True

        return admin

    #修改密码同样需要加密
    def update(self, instance, validated_data):
        admin = super().update(instance,validated_data)
        password = validated_data['password']
        #只有密码不是原密码且密码不为空的时候才会修改成功
        if (password is not None) and (not admin.check_password(password)):
            admin.set_password(password)
            admin.save()
        return instance



#频道管理
class GoodsChannel_Serializer(serializers.ModelSerializer):
    group = serializers.StringRelatedField(read_only=True)
    group_id = serializers.IntegerField()
    category = serializers.StringRelatedField(read_only=True)
    category_id = serializers.IntegerField()
    class Meta:
        model = GoodsChannel
        fields = '__all__'

# 新增频道中的频道组显示
class GoodsChannelGroup_Serializer(serializers.ModelSerializer):
    group = serializers.StringRelatedField(read_only=True)
    group_id = serializers.IntegerField()
    class Meta:
        model = GoodsChannel
        fields = '__all__'



#品牌管理视图集 序列化器
class Brand_Serializer(serializers.ModelSerializer):

    class Meta:
        model = Brand
        fields = '__all__'
